package com.hcb.tonguetiphealth.manager;

import android.content.Context;
import android.os.Handler;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.util.MessageWhat;

public class Location {

	public LocationClient mLocationClient = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	public static String TAG = "LocTestDemo";
	private int gpsOrNet = LocationClientOption.GpsFirst;
	private Handler handler;
	private double latitude;
	private double longitude;
	private String city;
	public Location(Context context, Handler handler){
		this.handler = handler;
		mLocationClient = new LocationClient(context);
		mLocationClient.registerLocationListener( myListener );
		setLocationOption();
	}
 
	
	/**
	 * 监听函数，获取到gps信息时结束
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			 
			StringBuffer sb = new StringBuffer(256);
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			if (latitude == 4.9E-324 || longitude == 4.9E-324) {
				return;
			}
			sb.append("经纬度：latitude:"+latitude +":longitude:"+longitude );
			if (location.getLocType() == BDLocation.TypeGpsLocation){
				if (Global.DEBUG) {
					System.out.println("gps定位");
				}
				
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
//				sb.append("\n省：");
//				sb.append(location.getProvince());
//				sb.append("\n市：");
//				sb.append(location.getCity());
//				sb.append("\n区/县：");
//				sb.append(location.getDistrict());]
				city = location.getCity();
			}
			sb.append("城市：city:"+city);
			System.out.println("定位信息:"+sb);
			handler.sendEmptyMessage(MessageWhat.BAIDU_LAOCATION_SUCESS);
		}
		
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null){
				return ; 
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("Poi time : ");
			sb.append(poiLocation.getTime());
			sb.append("\nerror code : "); 
			sb.append(poiLocation.getLocType());
			sb.append("\nlatitude : ");
			sb.append(poiLocation.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(poiLocation.getLongitude());
			sb.append("\nradius : ");
			sb.append(poiLocation.getRadius());
			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(poiLocation.getAddrStr());
			} 
			if(poiLocation.hasPoi()){
				sb.append("\nPoi:");
				sb.append(poiLocation.getPoi());
			}else{				
				sb.append("noPoi information");
			}
		}
	}
	//设置相关参数
	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);				//打开gps
		option.setCoorType("bd09ll");		//设置坐标类型
		option.setServiceName("com.baidu.location.service_v2.9");
	 		
		option.setScanSpan(1000);//设置定位模式，小于1秒则一次定位;大于等于1秒则定时定位
		option.setAddrType("all");//设置返回地址
		option.setPriority(gpsOrNet);        //不设置，默认是gps优先

//		option.setPoiNumber(10);
		option.disableCache(true);		
		mLocationClient.setLocOption(option);
	}
	public int getGpsOrNet() {
		return gpsOrNet;
	}
	public void setGpsOrNet(int gpsOrNet) {
		this.gpsOrNet = gpsOrNet;
	}
	public MyLocationListenner getMyListener() {
		return myListener;
	}
	public void setMyListener(MyLocationListenner myListener) {
		this.myListener = myListener;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	
}